<?php
/**
 * Created by MY_MIND.
 * User: WDJ
 * Date: 2018/7/5 0005
 * Time: 14:55
 */
namespace app\index\model;

use \think\Validate;
use \think\Db;

/**
 * 验证来自客户端的数据
 * @package app\admin\model
 */
class Client extends Validate
{
    //验证规则
    protected $rule    =   [
//        'token'        =>   'require|checkToken:ifRight',
        'uid'          =>   'require|checkUid:ifExist',
//        'isBind'       =>   'require|in:true,false',
//        'code'         =>   'requireIf:isBind,true|\d{6}|checkCode:ifConne',
        'type'         =>   'require|in:IOS,Android'
    ];

    //自定义验证规则
    public function checkUid($value, $rule, $data)
    {
        //在数据库查找是否有该Uid
        $result    =    getSqlConnec('QPAccountsDB_CHUI', 'AccountsInfo')->where(['UserID' => $value])->find();
        if($result){
            return true;
        }
        return '游戏玩家不存在';
    }

    //验证token
    public function checkToken($value, $rule, $data)
    {
        if(strtolower(md5($_SERVER['REQUEST_TIME'])) !== strtolower($value)){
            return 'token字段错误';
        }
        return true;
    }

    //验证邀请码
    public function checkCode($value , $rule, $data)
    {
        $result =   Db::name('relation')->where(['game_user_id' => $data['uid'] , 'invitation_code' => $value])->find();
        if($result){
            return true;
        }
        return '邀请码错误';
    }
}
